Autocapitalize the start of every sentence
Autocapitalizing the start of every sentence, a common function in cell phones, is not as trivial as one would think. Approaches such as mapping ". a" to ". A" in insert mode, using abbreviate, do not work, produce unexpected delay, or require extra keypresses. The strategy here is to interrupt insert mode on certain key presses, such as ., ?, !, as well as commands that start insert mode (a, A, i, o, etc.) and to catch subsequent keypresses with getchar(). Note that his script will load the relevant key mappings, via an autocommand, only when a file ending with *.txt is being edited. The Vim cuteness here consists of: *getchar – wait for a single keypress, return the numeric key code. One side effect of this function is that pressing Esc will always produce a brief delay, since the function sees Esc as a modifier. To skip this delay, you can perhaps modify the script slightly to also accept, say, "@" as cancel. *Ctrl-R= - In insert mode, pressing Ctr-R and then = allows one to execute a function, insert the result, and then return to insert mode. This trick is used to return to insert mode. An alternative would be to use "startinsert", but one nice effect of the Ctrl-R method is that abbreviations (eg, "ab im I'm") still work! (Vim regards using a normal command and then starting insert as an interruption and won't trigger abbreviations. However, using Ctrl-R to call a function that executes normal commands is an odd loophole.) *"/ " - It is possible using Ctrl-R to "insert" a delete command. One way this is helpful is that one can avoid various end-of-line complications by inserting this buffer character, "_". Script The following script can be placed in your vimrc. (Pressing escape cancels capitalize) fun! CapWait(prev) redraw let next=nr2char(getchar()) if next=~'[.?!\r\n:blank:()]' exe 'normal! i' . next . "\" return CapWait(next) elseif next=~'\e' return "\ " elseif a:prev=~'[\r\n:blank:]' return toupper(next) . "\ " else return next . "\ " endif endfun fun! CapHere() let trunc = getline(".")0:col(".")-2 return col(".") 1 ? CapWait("\r") \ : trunc=~'?!.\s*$\|^\s*$' ? CapWait(trunc-1:-1) : "\ " endfun fun! InitTextFile() im . ._=CapWait('.') im ? ?_=CapWait('?') im ! !_=CapWait('!') im _=CapWait("\r") im _=CapWait("\n") nm O O_=CapWait("\r") nm o o_=CapWait("\r") nm a a_=CapHere() nm A $a_=CapHere() nm i i_=CapHere() nm I I_=CapHere() nm s s_=CapHere() nm cc cc_=CapHere() nm cw cw_=CapHere() nm R R_=CapHere() nm C C_=CapHere() endfun au BufNewFile,BufRead *.txt call InitTextFile() Comments Amazing. I have not read the code, but I guess it would always be active? Wouldn't you want a way to turn it on/off? JohnBeckett 01:00, March 4, 2012 (UTC) :Yeah, you're right, it's always active, and a future version should probably include an easy way to unmap and remove the autocommands, although that might increase the complexity even more. I use my vim on my cell phone a lot, and I'm not really sure how common that is and how useful people may find this, but it might be interesting to some as a way to use getchar() to create a "new mode" or a more interactive vim. -- q335r49 I agree with this.